package cn.xyy.system.api;

import cn.xyy.system.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.xyy.common.domain.system.auth.PermissionVO;
import cn.xyy.common.annotation.log.Log;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.xyy.common.algorithm.MenuAlgorithm;
import cn.xyy.common.domain.mvc.R;

/**
 * 权限接口
 * 
 * @author xyy
 *
 */
@RestController
@RequestMapping(value = "/rest/permission")
public class PermissionRest {

	@Autowired
	private PermissionService permissionService;

	/**
	 * 获取所有权限
	 *
	 * @return
	 */
	@SaCheckPermission("permission:list")
	@RequestMapping(value = "getPermissionList")
	public R getPermissionList() {
		return R.ok(MenuAlgorithm.tree(permissionService.getPermissionList()));
	}

	/**
	 * 根据角色id获取对应的权限
	 *
	 * @param @param  roleId
	 * @param @return 设定文件
	 * @return ResponseResult 返回类型
	 */
	@SaCheckPermission("permission:list")
	@RequestMapping(value = "getPermissionListByRole")
	public R getPermissionListByRole(@RequestParam("roleId") String roleId) {
		return R.ok(permissionService.getPermissionListByRole(new String[] { roleId }));
	}

	@SaCheckPermission("permission:add")
	@Log("新增权限")
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public R add(@RequestBody PermissionVO vo) {
		permissionService.add(vo);
		return R.ok();
	}

	@SaCheckPermission("permission:edit")
	@Log("修改权限")
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public R update(@RequestBody PermissionVO vo) {
		permissionService.update(vo);
		return R.ok();
	}

	@SaCheckPermission("permission:del")
	@Log("删除权限")
	@RequestMapping(value = "delete")
	public R delete(@RequestParam("id") String id) {
		permissionService.delete(id);
		return R.ok();
	}

}
